<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      :title="
        dataForm.details ? '详情' : !dataForm.id ? $t('add') : $t('update')
      "
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        class=""
        :model="dataForm"
        ref="dataForm"
        :rules="dataRule"
        @keyup.enter.native="dataFormSubmitHandle()"
        :label-width="$i18n.locale === 'en-US' ? '140px' : '130px'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="码头名称" prop="wharfName">
              <el-input
                v-model="dataForm.wharfName"
                placeholder="码头名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经纬度" prop="lonandLat">
              <el-input
                v-model="dataForm.lonandLat"
                style="width: 60%; margin-right: 10px"
              ></el-input>
              <el-button type="primary" size="mini" @click="mapCli()"
                >地图选点</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置" prop="wharfPosition">
              <el-input
                v-model="dataForm.wharfPosition"
                placeholder="位置"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规模" prop="wharfScale">
              <el-input
                v-model="dataForm.wharfScale"
                placeholder="规模"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="wharfPhone">
              <el-input
                v-model="dataForm.wharfPhone"
                placeholder="联系方式"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="介绍" prop="wharfIntroduce">
              <editor-vue
                style="margin-top: -30px"
                :content="dataForm.wharfIntroduce"
                @EditorChange="getEditor"
                ref="infoIntroduceRef"
              ></editor-vue>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="封面(最多1张)" prop="pic">
              <el-upload
                class="avatar-uploader"
                :action="url"
                :show-file-list="false"
                :on-success="
                  (res, file, fileList) => successHandle1(res, file, fileList)
                "
                :before-upload="beforeUploadHandle"
              >
                <img v-if="dataForm.pic" :src="dataForm.pic" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template slot="footer">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
          $t("confirm")
        }}</el-button>
      </template>
    </el-dialog>
    <el-dialog :visible.sync="mapShow" title="地图选点">
      <mapdiv
        ref="mapDiv"
        style="width: 96%; margin: 0 auto 20px"
        :longitude="dataForm.longitude"
        :latitude="dataForm.latitude"
        @getPos="getPosVal($event)"
      >
      </mapdiv>
    </el-dialog>
    <!-- 裁剪框 -->
    <cropper-dlg
      v-if="showCropper"
      :dialogVisible.sync="showCropper"
      :img-file="currentFile"
      ref="vueCropper"
      @upload="cropperUpload"
    ></cropper-dlg>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import debounce from "lodash/debounce";
import editorVue from "@/components/editor";
import mapdiv from "../data/map-data/map2";
// 富文本编辑器
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

import { quillEditor } from "vue-quill-editor";
export default {
  components: {
    editorVue,
    mapdiv,
  },
  data() {
    return {
      currentFile: "",
      cropperIndex: null,
      showCropper: false, //裁剪弹框
      isCropper: true, //上传之前是否显示裁剪
      mapShow: false,
      visible: false,
      fileList: [],
      url: `${
        window.SITE_CONFIG["apiURL"]
      }/oss/file/aliyunUpload?access_token=${Cookies.get("access_token")}`,
      dataForm: {
        id: "",
        lonandLat: "",
        latitude: "",
        longitude: "",
        pic: "",
        wharfIntroduce: "",
        wharfName: "",
        wharfPhone: "",
        wharfPosition: "",
        wharfScale: "",
      },
      dataRule: {
        wharfName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        wharfPosition: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        lonandLat: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        pic: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  beforeDestroy() {
    if (!this.map) {
      return;
    }
    this.map.destroy();
    this.map = null;
  },
  methods: {
    getEditor(val) {
      this.dataForm.wharfIntroduce = val;
    },
    mapCli() {
      this.mapShow = true;
      this.$nextTick(() => {
        this.$refs.mapDiv.initMap();
      });
    },
    getPosVal(obj) {
      this.mapShow = false;
      this.dataForm.latitude = obj.lat;
      this.dataForm.longitude = obj.lon;
      this.dataForm.lonandLat = obj.lon + "," + obj.lat;
    },
    // 上传成功
    successHandle1(res, file, fileList) {
      if (res.code !== 0) {
        return this.$message.error(res.msg);
      }
      this.dataForm.pic = res.data.url;
    },
    // 上传之前
    beforeUploadHandle(file) {
      if (
        file.type !== "image/jpg" &&
        file.type !== "image/jpeg" &&
        file.type !== "image/png" &&
        file.type !== "image/gif"
      ) {
        this.$message.error(
          this.$t("upload.tip", {
            format: "jpg、png、gif",
          })
        );
        return false;
      }
      if (this.isCropper) {
        //显示裁剪
        this.currentFile = file;
        this.cropperIndex = name;
        this.showCropper = true;
        this.$nextTick(() => {
          this.$refs.vueCropper.init();
        });
        return false;
      }
    },
    cropperUpload(data) {
      this.showCropper = false;
      if (data != "close") {
        //裁剪成功后获取的图片
        this.$http.post("/oss/file/aliyunUpload", data).then((res) => {
          if (res.data.code == 0) {
            this.successHandle1(res.data);
          }
        });
      }
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.dataForm.latitude = "";
        this.dataForm.longitude = "";
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.$nextTick(() => {
            this.$refs.infoIntroduceRef.init();
          });
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/travel/car/wharfGeneralWharfIntel/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.dataForm.lonandLat =
            this.dataForm.longitude + "," + this.dataForm.latitude;
          this.$nextTick(() => {
            this.$refs.infoIntroduceRef.init();
          });
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/travel/car/wharfGeneralWharfIntel",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style>
.edit {
  width: 600px;
  height: 300px;
  border: 2px solid #000;
}
</style>
